import React, { Component } from 'react';

class Boundary extends Component {
  constructor() {
    super();
    this.state = {
      // 是否有报错
      hasError: false
    }
  }
  componentDidCatch() {
    // 在子组件报错时自动执行
    this.setState({
      hasError: true
    })
  }
  render() {
    return (
      <div>
        {/* 报错时显示mark，正常时显示子组件 */}
        {
          this.state.hasError?<mark>报错了...</mark>:this.props.children
        }
      </div>
    );
  }
}

export default Boundary;
